package com.example.kaogong.sys.controller;


import com.example.kaogong.common.exception.ParamsErrorException;
import com.example.kaogong.common.utils.Assert;
import com.example.kaogong.common.utils.RespBean;
import com.example.kaogong.sys.pojo.dto.PaperDto;
import com.example.kaogong.sys.pojo.dto.PaperQuestionDto;
import com.example.kaogong.sys.pojo.vo.PaperDetailInfoVo;
import com.example.kaogong.sys.pojo.vo.PaperInfoVo;
import com.example.kaogong.sys.pojo.vo.QuestionVo;
import com.example.kaogong.sys.service.IPaperService;
import com.example.kaogong.sys.service.IQuestionService;
import com.google.gson.Gson;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.security.Principal;
import java.util.List;

/**
 * <p>
 * 试卷 前端控制器
 * </p>
 *
 * @author    
 * @since 2022-03-26
 */
@RestController
@RequestMapping("/paper")
@Api(tags = "试卷管理接口")
@Slf4j
public class PaperController {

    @Autowired
    private IQuestionService questionService;

    @Autowired
    private IPaperService paperService;

    @ApiOperation(value = "获取所有已发布试卷的基本信息")
    @GetMapping(value = "/info/all/published")
    public RespBean<List<PaperInfoVo>> getAllPublishedPaper() {
        RespBean<List<PaperInfoVo>> respBean = paperService.getAllPublishedPaper();
        return respBean;
    }

    @ApiOperation(value = "根据试卷编号获取试卷详细信息")
    @GetMapping(value = "/info/detail/{paperId}")
    public RespBean<PaperDetailInfoVo> getPaperDetailInfoById(@PathVariable Integer paperId) {
        Assert.notNull(paperId, new ParamsErrorException("试卷编号不能空"));
        log.info("paperId:{}", paperId);
        RespBean<PaperDetailInfoVo> respBean = paperService.getPaperDetailInfoById(paperId);
        return respBean;
    }

    @ApiOperation(value = "获取当前教师所有未发布试卷")
    @GetMapping(value = "/info/draft")
    public RespBean<List<PaperInfoVo>> getTeacherAllUnpublishedPaper(Principal principal) {
        if (principal == null)
            return RespBean.error();
        Assert.notBlank(principal.getName(), new ParamsErrorException("用户不存在"));
        RespBean<List<PaperInfoVo>> respBean = paperService.getTeacherAllPaper(principal.getName(), 1);
        return respBean;
    }

    @ApiOperation(value = "获取当前教师所有已发布试卷")
    @GetMapping(value = "/info/published")
    public RespBean<List<PaperInfoVo>> getTeacherAllPublishedPaper(Principal principal) {
        if (principal == null)
            return RespBean.error();
        Assert.notBlank(principal.getName(), new ParamsErrorException("用户不存在"));
        RespBean<List<PaperInfoVo>> respBean = paperService.getTeacherAllPaper(principal.getName(), 2);
        return respBean;
    }

    @ApiOperation(value = "获取题库中某种题型的全部试题", notes = "1-单选，2-多选，3-判断题，4-填空题，5-主观论述题")
    @GetMapping(value = "/question/{type}")
    public RespBean<List<QuestionVo>> getAllQuestionByType(@PathVariable Integer type) {
        Assert.notNull(type, new ParamsErrorException("题目类型不能为空"));
        log.info("type:{}", type);
        RespBean<List<QuestionVo>> respBean = questionService.getAllQuestionByType(type);
        log.info("response:{}", new Gson().toJson(respBean));
        return respBean;
    }

    @ApiOperation(value = "教师创建试卷")
    @PostMapping(value = "/manage/create")
    public RespBean createPaper(@Valid @RequestBody PaperDto paperDto, Principal principal) {
        if (principal == null)
            return RespBean.error();
        Assert.notBlank(principal.getName(), new ParamsErrorException("用户不存在"));
        log.info("request:{}", new Gson().toJson(paperDto));
        RespBean respBean = paperService.createPaper(paperDto, principal.getName());
        return respBean;
    }

    @ApiOperation(value = "添加试卷题目", notes = "只能为草稿状态的试卷添加题目")
    @PostMapping(value = "/manage/addQuestion/{paperId}")
    public RespBean addPaperQuestion(@RequestBody List<PaperQuestionDto> paperQuestionDtoList, @PathVariable Integer paperId) {
        Assert.notNull(paperId, new ParamsErrorException("试卷编号不能为空"));
        log.info("paperId:{}, request:{}", paperId, new Gson().toJson(paperQuestionDtoList));
        RespBean respBean = paperService.addPaperQuestion(paperQuestionDtoList, paperId);
        return respBean;
    }

    @ApiOperation(value = "删除试卷题目", notes = "只能删除草稿状态的试卷题目")
    @DeleteMapping(value = "/manage/deleteQuestion/{paperId}")
    public RespBean deletePaperQuestion(@RequestBody List<PaperQuestionDto> paperQuestionDtoList, @PathVariable Integer paperId) {
        Assert.notNull(paperId, new ParamsErrorException("试卷编号不能为空"));
        log.info("paperId:{}, request:{}", paperId, new Gson().toJson(paperQuestionDtoList));
        RespBean respBean = paperService.deletePaperQuestion(paperQuestionDtoList, paperId);
        return respBean;
    }

    @ApiOperation(value = "试卷发布")
    @PostMapping("/publish/{paperId}")
    public RespBean publishPaperById(@PathVariable Integer paperId) {
        Assert.notNull(paperId, new ParamsErrorException("试卷编号不能为空"));
        log.info("paperId:{}", paperId);
        RespBean respBean = paperService.publishPaperById(paperId);
        return respBean;
    }

    @ApiOperation(value = "删除试卷")
    @DeleteMapping(value = "/{paperId}")
    public RespBean deletePaperById(@PathVariable Integer paperId) {
        Assert.notNull(paperId, new ParamsErrorException("试卷编号不能为空"));
        log.info("paperId:{}", paperId);
        RespBean respBean = paperService.deletePaperById(paperId);
        return respBean;
    }

}
